// pages/paymethod/paymethod.js
const app = getApp();
import request from "../../utils/request";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    radio: "1",
    price: "",
    payCode: "",
    show: false,
    payID:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const payPrice = options.price / 100;
    this.setData({
      price: payPrice,
      payCode: options.payOrderId,
      payID:options.id
    });
  },
  goback() {
    wx.showModal({
      title: "提示",
      content: "您确定要离开收银台?",
      cancelText: "继续支付",
      confirmText: "确认离开",
      confirmColor: "#ff0000",
      complete: (res) => {
        if (res.cancel) {
          console.log("用户点击取消");
        }

        if (res.confirm) {
          wx.navigateBack({
            delta: 1,
          });
        }
      },
    });
  },
  async gopay() {
    const openid = wx.getStorageSync("openid");
    const res = await request({
      url: app.globalData.baseURL + "/app-user-api/pay/order/submit",
      method: "POST",
      data: {
        id: this.data.payCode,
        channelCode: "wx_lite",
        channelExtras: {
          openid: openid,
        },
        displayMode: "url",
        returnUrl: "",
      },
    });
    if (res.code === 0) {
      const num = JSON.parse(res.data.displayContent);
      wx.requestPayment({
        timeStamp: num.timeStamp,
        nonceStr: num.nonceStr,
        package: num.packageValue,
        signType: num.signType,
        paySign: num.paySign,
        success:(res) => {
          wx.navigateTo({
            url: `/pages/refund/refund?checkID=${this.data.payID}`,
          });
        },
        fail(res) {
          console.log(res);
        },
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    return {
      title: "嘉之勤官网小程序",
      path: "/pages/index/index",
      imageUrl:
        "https://youyougo2.oss-cn-chengdu.aliyuncs.com/0201506f6fc0f4145e34159c2e2ac66b94b96450dd29e5a19d177edc5dad8023.png",
    };
  },
});
